/*
 * Copyright (c) 2016 MaxSky.TK. All Rights Reserved.
 */

package cn.studyjams.s1.sj47.maxsky;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import java.util.Timer;
import java.util.TimerTask;

public class LauncherActivity extends AppCompatActivity {

	private Activity mActivity;
	TimerTask task = new TimerTask() {
		public void run() {
			//execute the task
			Intent intent = new Intent(mActivity, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(intent);
			finish();
		}
	};
	private ViewFlipper viewFlipper;
	private GestureDetector gestureDetector;
	private OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			if ((e2.getX() - e1.getX()) < -100) {
				Animation lInAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_left_in);
				Animation lOutAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_left_out);
				viewFlipper.setInAnimation(lInAnim);
				viewFlipper.setOutAnimation(lOutAnim);
				viewFlipper.showNext();
				new Timer().schedule(task, 950);
			}
			return super.onFling(e1, e2, velocityX, velocityY);
		}


		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			return super.onScroll(e1, e2, distanceX, distanceY);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_launcher);
		mActivity = LauncherActivity.this;
		viewFlipper = (ViewFlipper) findViewById(R.id.launch_view_flipper);
		ImageView imageView = new ImageView(mActivity);
		imageView.setImageResource(R.drawable.lc_transit);
		imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		imageView.setBackgroundColor(Color.parseColor("#FFFFFF"));
		viewFlipper.addView(imageView, new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		gestureDetector = new GestureDetector(mActivity, onGestureListener);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}

}